/*
 * Copyright (C) 2001-2005 Pleasant nightmare studio
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.pleasantnightmare.acake.errorhandling;

import javax.annotation.processing.Messager;
import javax.tools.Diagnostic;
import java.text.MessageFormat;

/**
 * Uses Messager from APT environment.
 *
 * @author deus
 * @version 1.0
 * @since Oct 22, 2010 2:11:17 PM
 */
public class DefaultErrorHandler implements ErrorHandler {
    private Messager messager;

    public DefaultErrorHandler() {
    }

    public DefaultErrorHandler(Messager messager) {
        if (messager == null)
            throw new IllegalArgumentException("messager = null");
        this.messager = messager;
    }

    public void setMessager(Messager messager) {
        this.messager = messager;
    }

    @Override
    public void report(CheckError checkError) {
        if (messager == null)
            throw new IllegalStateException("messager = null");

        String line = MessageFormat.format("{0}: {1}\n", checkError.getErrorCode(), checkError.getMessage());

        if (checkError.getLevel() < CheckError.DEFAULT_INFO_LEVEL)
            messager.printMessage(Diagnostic.Kind.NOTE, line);
        else if (checkError.getLevel() < CheckError.DEFAULT_WARN_LEVEL)
            messager.printMessage(Diagnostic.Kind.WARNING, line);
        else
            messager.printMessage(Diagnostic.Kind.ERROR, line);
    }

    @Override
    public void printMessage(Diagnostic.Kind note, String message) {
        messager.printMessage(note, message);
    }
}
